题目
You are given an array (which will have a length of at least 3, but could be very large) containing integers. The array is either entirely comprised of odd integers or entirely comprised of even integers except for a single integer N
. Write a method that takes the array as an argument and returns this “outlier” N
.
Examples
1 | [2, 4, 0, 100, 4, 11, 2602, 36] |
思路
最直接的思路就是遍历数组,前提要判断我们要找的目标是一个奇数还是偶数。根据数组中的前三个数字可以判断出目标数是奇还是偶。然后遍历数组,找到目标数。
答案
我的答案
1 | def find_outlier(integers): |
最佳答案
1 | def find_outlier(int): |
最佳答案的思路是分别将奇数和偶数筛选出为单独的list,然后比较最终的list大小可以得出目标数。该方法看着简洁明了。